package cn.otrue.patienthealthmanager;

import android.util.Log;
import cn.otrue.patienthealthmanager.utils.PushUtils;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.baidu.frontia.FrontiaApplication;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class HealthApplication extends FrontiaApplication {
	private static HealthApplication mInstance = null;
	private LocationClient locationClient;
	private BDLocationListener locationListener;

	private int isSigned = -1;

	private double latitude, longitude;
	private String cityName = "";

	@Override
	public void onCreate() {
		super.onCreate();
		// SDKInitializer.initialize(this);
		ImageLoaderConfiguration configuration = ImageLoaderConfiguration
				.createDefault(this);
		ImageLoader.getInstance().init(configuration);
		mInstance = this;
		PushManager.startWork(getApplicationContext(),
				PushConstants.LOGIN_TYPE_API_KEY,
				PushUtils.getMetaValue(this, "push_api_key"));
		// 初始化友盟统计
		// AppTrack.initAppTrack(this, true, false);
		// CrashHandler crashHandler = CrashHandler.getInstance();
		// crashHandler.init(this);
		BaiduLocation();
	}

	/**
	 * 初始化AMap对象
	 */
	public void BaiduLocation() {
		try {

			locationClient = new LocationClient(getApplicationContext());
			LocationClientOption locOption = new LocationClientOption();
			locOption.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
			locOption.setCoorType("bd09ll");// 设置定位结果类型
			locOption.setScanSpan(999);// 设置发起定位请求的间隔时间,ms
			locOption.setIsNeedAddress(true);// 返回的定位结果包含地址信息
			locOption.setNeedDeviceDirect(true);// 设置返回结果包含手机的方向

			locationListener = new MyLocationListener();
			locationClient.setLocOption(locOption);
			locationClient.registerLocationListener(locationListener);
			locationClient.start();
			// locationClient.requestLocation();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class MyLocationListener implements BDLocationListener {
		// 地图定位
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null) {
				return;
			}

			longitude = location.getLongitude();
			latitude = location.getLatitude();
			cityName = location.getCity();
			Log.i("longitude", String.valueOf(longitude));
			Log.i("latitude", String.valueOf(latitude));
		}

	}

	public int getisSigned() {
		return isSigned;
	}

	public void setisSigned(int isSigned) {
		this.isSigned = isSigned;
	}

	public static HealthApplication getInstance() {
		return mInstance;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

}
